﻿using DM_IBLL;
using DM_Sqlite.MappingClass;
using DM_TOOLS.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System.Reflection;

namespace DMSystem_Sqlite_WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    [ApiController]
    public class QuartzSupplementController : ControllerBase
    {
        private readonly ILogger<QuartzSupplementController> _logger;
        private readonly IBLL_User _iBLL_User;
        private readonly IBLL_DBCUser _iBLL_DBCUser;
        //private readonly string controllerName;
        private readonly IMemoryCache _memoryCache;//内存缓存
        public QuartzSupplementController(ILogger<QuartzSupplementController> logger, IMemoryCache memoryCache, IBLL_User iBLL_User, IBLL_DBCUser iBLL_DBCUser)
        {
            _logger = logger;
            _memoryCache = memoryCache;
            _iBLL_User = iBLL_User;
            _iBLL_DBCUser = iBLL_DBCUser;
            //controllerName = this.GetType().Name;
        }

        /// <summary>
        /// 获得准确的里程数据（计算路径下所有csv文件的总里程并返回给前端）
        /// </summary>
        /// <param name="jobname"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetTotalMileage(string path)
        {
            var vehicle = _iBLL_User.LoadEntities(a => a.watchpath == path).FirstOrDefault()?.vehicle;
            var odo = _iBLL_User.LoadEntities(a => a.watchpath == path).FirstOrDefault()?.odo;

            if (!string.IsNullOrEmpty(vehicle)&& !string.IsNullOrEmpty(odo))
            {
                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                double sum = await watchpath.GetTotalMileagesfromODO(odo);
                _memoryCache.Set<double>(vehicle + DateTime.Now.ToString("MM-dd") + "sumdistance", sum);
                return Ok(sum);
            }
            else
            {
                return BadRequest("公共参数没有设置");
            }
        }
        /// <summary>
        /// 用于报文监控的方法
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetDBCTotalMileage(string path)
        {
            var vehicle = _iBLL_DBCUser.LoadEntities(a => a.csvpath == path).FirstOrDefault()?.vehicle;
            var odo = _iBLL_DBCUser.LoadEntities(a => a.csvpath == path).FirstOrDefault()?.odo;

            if (!string.IsNullOrEmpty(vehicle) && !string.IsNullOrEmpty(odo))
            {
                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                double sum = await watchpath.GetTotalMileagesfromODO(odo);
                _memoryCache.Set<double>(vehicle + DateTime.Now.ToString("MM-dd") + "sumdistance", sum);
                return Ok(sum);
            }
            else
            {
                return BadRequest("公共参数没有设置");
            }
        }
        /// <summary>
        /// 获得准确的轨迹数据（计算路径下所有csv文件的总轨迹并返回给前端）
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetTotalTracks(string path)
        {
            //var t = MethodBase.GetCurrentMethod();
            //string? methodName = t?.Name;
            var adminprop = _memoryCache.Get<dm_props_admin>("datamonitoradminpara");
            var vehicle = _iBLL_User.LoadEntities(a => a.watchpath == path).FirstOrDefault()?.vehicle;
            if (adminprop != null && !string.IsNullOrEmpty(vehicle))
            {

                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                var gps = await watchpath.GetTotalTracksfromCSVsV2(adminprop.reducetimes);

                return Ok(gps);

            }
            else
            {
                _logger.LogWarning($"{vehicle}车辆路径为{path}在复原轨迹时公共参数设置有误");
                return BadRequest("复原轨迹公共参数设置有误");
            }
        }
        /// <summary>
        /// 用于报文监控的方法
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetDBCTotalTracks(string path)
        {
            //var t = MethodBase.GetCurrentMethod();
            //string? methodName = t?.Name;
            var adminprop = _memoryCache.Get<dm_props_admin>("datamonitoradminpara");
            var vehicle = _iBLL_DBCUser.LoadEntities(a => a.csvpath == path).FirstOrDefault()?.vehicle;
            if (adminprop != null && !string.IsNullOrEmpty(vehicle))
            {

                string watchpath = Path.Combine(path, DateTime.Now.ToString("MM_dd"));
                var gps = await watchpath.GetTotalTracksfromCSVsV2(adminprop.reducetimes);

                return Ok(gps);

            }
            else
            {
                _logger.LogWarning($"{vehicle}车辆路径为{path}在复原轨迹时公共参数设置有误");
                return BadRequest("复原轨迹公共参数设置有误");
            }
        }
    }
}
